import { service } from "./service";

import { config } from "./config";

const { default_headers } = config;

const request = (option) => {
  const { url, method, params, data, headersType, responseType, ...config } = option;
  return service({
    url: url,
    method,
    params,
    data,
    ...config,
    responseType: responseType,
    headers: {
      "Content-Type": headersType || default_headers,
    },
  });
};
export default {
  get: async (option) => {
    const res = await request({ method: "GET", ...option });
    console.log(
      "%curl%o",
      "color: white; background-color: #f1738e; padding: 2px 5px; border-radius: 2px",
      option.url
    );
    console.log(
      "%c返参%o",
      "color: white; background-color: #61ee98; padding: 2px 5px; border-radius: 2px",
      res?.data
    );
    return res.data;
  },
  post: async (option) => {
    const res = await request({ method: "POST", ...option });
    console.log(
      "%curl%o",
      "color: white; background-color: #f1738e; padding: 2px 5px; border-radius: 2px",
      option.url
    );
    console.log(
      "%c$o返参%o",
      "color: white; background-color: #61ee98; padding: 2px 5px; border-radius: 2px",
      res?.data
    );
    return res.data;
  },
  postOriginal: async (option) => {
    const res = await request({ method: "POST", ...option });
    return res;
  },
  delete: async (option) => {
    const res = await request({ method: "DELETE", ...option });
    return res.data;
  },
  put: async (option) => {
    const res = await request({ method: "PUT", ...option });
    return res.data;
  },
  download: async (option) => {
    const res = await request({ method: "GET", responseType: "blob", ...option });
    return res;
  },
  upload: async (option) => {
    option.headersType = "multipart/form-data";
    const res = await request({ method: "POST", ...option });
    return res;
  },
};
